<?php
include_once 'category-order-list.php';
include_once 'constants.php';
// Add RSS links to <head> section
add_theme_support('automatic-feed-links');

//Post thumbnail
add_theme_support('post-thumbnails');
set_post_thumbnail_size(100, 100, true);
add_image_size('sub-sumb', 93, 72, true);
add_image_size('last-news', 285, 200, true);
//Add post formats
add_theme_support('post-formats', array('gallery', 'image', 'video'));
// Load jQuery
if (!is_admin()) {
    //wp_deregister_script('jquery');
    //wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"), false);
    //wp_enqueue_script('jquery');
    wp_enqueue_script('hoverIntent', get_template_directory_uri() . '/js/hoverIntent.js', array('jquery'));
    wp_enqueue_script('superfish', get_template_directory_uri() . '/js/superfish.js', array('jquery'));
    wp_enqueue_script('cycle', get_template_directory_uri() . '/js/cycle.js', array('jquery'));
    wp_enqueue_script('jquery.anythingslider', get_template_directory_uri() . '/js/anythingslider.js', array('jquery'));
    wp_enqueue_script('custom', get_template_directory_uri() . '/js/custom.js', array('jquery'));
}

// Clean up the <head>
function removeHeadLinks() {
    remove_action('wp_head', 'rsd_link');
    remove_action('wp_head', 'wlwmanifest_link');
}

add_action('init', 'removeHeadLinks');
remove_action('wp_head', 'wp_generator');

add_action('init', 'create_post_type');

function create_post_type() {
    register_post_type('members', array(
        'labels' => array(
            'name' => __('Members'),
            'singular_name' => __('Member')
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'members')
            )
    );
}

// Declare sidebar widget zone
if (function_exists('register_sidebar')) {
    //Header  ads
    register_sidebar(array(
        'name' => 'Header Banner Add',
        'id' => 'header-banner-ads',
        'description' => 'The header banner add',
        'before_widget' => '<div id="%1$s" class="banner-head %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>'
    ));

    //Right Sidebar ads
    register_sidebar(array(
        'name' => 'First Right Sidebar Ads',
        'id' => 'first-right-sidebar-ads',
        'description' => 'The top right sidebar ads at bottom',
        'before_widget' => '<div id="%1$s" class="right-ads %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>'
    ));
    register_sidebar(array(
        'name' => 'Second Right Sidebar Ads',
        'id' => 'second-right-sidebar-ads',
        'description' => 'The second rigd sidebar ads at bottom',
        'before_widget' => '<div id="%1$s" class="right-ads %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>'
    ));

    register_sidebar(array(
        'name' => 'New Magazine Cover',
        'id' => 'new-magazine-cover',
        'description' => 'The last new of Health Cambodia Magazine',
        'before_widget' => '<div>',
        'after_widget' => '</div>',
    ));

    register_sidebar(array(
        'name' => 'Related Post for Single Post Widgets',
        'id' => 'single-page-related-post',
        'description' => 'These are widgets for the sidebar.',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h2>',
        'after_title' => '</h2>'
    ));
    register_sidebar(array(
        'name' => 'Home banner ads first place',
        'id' => 'home-ads-first-place',
        'description' => 'the Widget for home banner ads',
        'before_widget' => '<div>',
        'after_widget' => '</div>',
    ));
    register_sidebar(array(
        'name' => 'Home banner ads second place',
        'id' => 'home-ads-second-place',
        'description' => 'the Widget for home banner ads',
        'before_widget' => '<div>',
        'after_widget' => '</div>',
    ));
    register_sidebar(array(
        'name' => 'Home banner ads third place',
        'id' => 'home-ads-third-place',
        'description' => 'the Widget for home banner ads',
        'before_widget' => '<div>',
        'after_widget' => '</div>',
    ));

    //Left Sidebar ads
    register_sidebar(array(
        'name' => 'First Left Sidebar Ads',
        'id' => 'first-ads-left-banner',
        'description' => 'The left sidebar ads at bottom',
        'before_widget' => '<div id="%1$s" class="left-ads img %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>'
    ));
    register_sidebar(array(
        'name' => 'Left Sidebar Ads',
        'id' => 'ads-left-banner',
        'description' => 'The left sidebar ads at bottom',
        'before_widget' => '<div id="%1$s" class="left-ads %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>'
    ));
    register_sidebar(array(
        'name' => 'Sidebar Widgets',
        'id' => 'sidebar-widgets',
        'description' => 'These are widgets for the sidebar.',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h2>',
        'after_title' => '</h2>'
    ));
}

/**
 * Display simple posts
 * 
 * @param int $main_post_id
 */
function display_posts($main_post_id) {
    $i = 1;
    while (have_posts()) : the_post();
        if ($main_post_id != get_the_ID())
            if ($i <= 2) {
                ?>

                <div <?php post_class() ?>>
                    <div class="post-thumbnail">
                        <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
                    </div>
                    <div class="post-content">
                        <h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>

                        <?php //include (TEMPLATEPATH . '/inc/meta.php' );   ?>

                        <div class="entry">
                            <p>
                                <?php echo get_the_excerpt(); ?>
                                ...<a class="readmore" href="<?php the_permalink(); ?>">អាន​លម្អិត</a>
                            </p>
                        </div>
                    </div>
                    <div class="clearfix"></div>

                </div>

                <?php
                $i++;
            }
    endwhile;
}

/**
 * Display main articles
 * 
 */
function main_article() {
    //the_post();
    
    $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
            $img = '<img src="'.TIMTHUMB_URL . $large_image_url[0] .'&amp;w=285&amp;h=200&amp;zc=1&amp;q=100&amp;a=c" alt="' . get_the_title() . '" />';
    ?>
    <div id="last_news_cat" class="clearfix">
        <div class="photo main-article">
            <a href="<?php the_permalink(); ?>"><?php echo $img ?></a>
        </div>
        <div class="last_topic">
            <h3><a href="<?php the_permalink(); ?>"><?php the_title() ?></a></h3>
            <div class="last_topic_summary">
                <p>
                    <?php echo get_the_excerpt(); ?>
                    ...<a href="<?php the_permalink(); ?>">អាន​លម្អិត</a>
                </p>
            </div>
        </div>
    </div>
    <?php
    return get_the_ID();
}

function relatePostByCategory($cat_ID, $post_ID) {
    query_posts("cat=$cat_ID&posts_per_page=11");
    ?>
    <ul class="related_post">
        <?php
        $i = 0;
        if (have_posts()) {
            while (have_posts()) : the_post();
                if ($post_ID != get_the_ID()) {
                    $i++;
                    ?>
                    <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
                    <?php
                    if ($n == 10)
                        break;
                }
            endwhile;
        }
        ?>
    </ul>
    <?php
    wp_reset_query();
}

/**
 * Popular posts
 * 
 *  
 */
function hc_set_post_views($postID) {
    $count_key = 'wpb_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if ($count == '') {
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    } else {
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

//To keep the count accurate, lets get rid of prefetching
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

/**
 * Tracking Post
 *  
 */
function hc_track_post_views($post_id) {
    if (!is_single())
        return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    hc_set_post_views($post_id);
}

//add_action( 'wp_head', 'hc_track_post_views');
// Create a new filtering function that will add our where clause to the query
function filter_where($where = '') {
    // posts  30 to 60 days old
    // posts in the last 30 days
    $where .= " AND post_date < '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
}

// RRR Added to get local time as per WP settings		
function curdate() {
    return gmdate('Y-m-d', ( time() + ( get_option('gmt_offset') * 3600 )));
}

/**
 *
 * @global type $wpdb
 * @param int $rang  total day to show
 */
function hc_popular_posts($rang = 7, $limit = 10) {
    global $wpdb;

    $table = $wpdb->prefix . "popularpostsdatacache";

    $query = "SELECT p.ID AS 'id', p.post_title AS 'title', p.post_date AS 'date', p.post_author AS 'uid' , IFNULL(v.pageviews, 0) AS 'pageviews' 
                FROM {$wpdb->posts} p 
                LEFT JOIN (
                    SELECT id, SUM(pageviews) AS 'pageviews', day 
                    FROM (
                            SELECT id, pageviews, day 
                            FROM {$table}
                            WHERE day > DATE_SUB('" . curdate() . "', INTERVAL {$rang} DAY) 
                            ORDER BY day
                    ) sv GROUP BY id
                ) v ON p.ID = v.id
                WHERE p.post_status = 'publish' AND p.post_password = '' AND v.pageviews > 0 AND p.post_type IN('post') GROUP BY p.ID ORDER BY v.pageviews DESC LIMIT {$limit};";
    $mostpopular = $wpdb->get_results($query);
    
    return $mostpopular;
}

?>